﻿/// <Summary> 
/// $Author: Nikhil Chinchwade 
/// This file Manages the subscription mechanism for the Monitoring Result observer
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MindTree.PerfWorkbench.Azure.Monitor.AzureMonLib
{
    /// <summary>
    /// This class manages all the subscriptions for Monitoring Result Observer
    /// </summary>
    public class PerfResultService  
    {
        private static List<PerfMonResultObserver> GraphEntityObservers = new List<PerfMonResultObserver>();

        /// <summary>
        /// Subscribe to Monitoring Results Data source
        /// </summary>
        /// <param name="paramObserver">Observer entity</param>
        public void Subscribe(PerfMonResultObserver paramObserver)
        {
           if(!GraphEntityObservers.Contains<PerfMonResultObserver>(paramObserver))
           {
               GraphEntityObservers.Add(paramObserver);
           }
        }

        /// <summary>
        /// Unsubscribe to Monitoring Results Data Source
        /// </summary>
        /// <param name="paramObserver">Observer Entity</param>
        public void UnSubscribe(PerfMonResultObserver paramObserver)
        {
           if(GraphEntityObservers.Contains<PerfMonResultObserver>(paramObserver))
           {
               GraphEntityObservers.Remove(paramObserver);
           } 
        } 

        /// <summary>
        /// Notify all the observers when monitoring result is updated 
        /// </summary>
        public static void Notify()
        {
            foreach (PerfMonResultObserver observer in GraphEntityObservers)
            {                
                observer.UpdateData();
            }
        }
    }
}
